import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:full_display/page/login/register/register_api.dart';

class RegisterProvider extends ChangeNotifier {
  static const titleList = ['手机号注册', '邮箱注册'];

  var registerApi = RegisterApi();

  ///校验是否是正确的手机号
  bool regPhone(String phoneNum) {
    return RegExp(r'^1[3456789]\d{9}$').hasMatch(phoneNum);
  }

  ///是否是合规的邮箱
  bool isValidEmail(String email) {
    return RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(email);
  }

  final phoneMap = {'phoneNum': '', 'password': '', 'passwordConfig': '', 'verificationCode': '', 'areaCode': '+86'};
  final emailMap = {'Email': '', 'password': '', 'passwordConfig': '', 'verificationCode': ''};

  ///更新手机注册的参数
  updatePhoneMap(map) {
    phoneMap['phoneNum'] = map['phoneNum'];
    phoneMap['password'] = map['password'];
    phoneMap['passwordConfig'] = map['passwordConfig'];
    phoneMap['verificationCode'] = map['verificationCode'];
    phoneMap['areaCode'] = map['areaCode'];
  }
  ///更新注册的参数
  updateEmailMap(map) {
    emailMap['Email'] = map['Email'];
    emailMap['password'] = map['password'];
    emailMap['passwordConfig'] = map['passwordConfig'];
    emailMap['verificationCode'] = map['verificationCode'];
  }


  registerEmailCode(Function tip) async {
    var Email = emailMap['Email'] ?? '';
    if (Email.isEmpty || !isValidEmail(Email)) {
      tip.call('请输入正确的邮箱');
      return;
    }
    var password = emailMap['password'] ?? '';
    if (password.isEmpty) {
      tip.call('请输入密码');
      return;
    }

    var passwordConfig = emailMap['passwordConfig'] ?? '';
    if (passwordConfig.isEmpty || passwordConfig != password) {
      tip.call('确认密码不能为空或者需密码不一致');
      return;
    }

    await registerApi.postRegister('', '${emailMap['Email']}', password, passwordConfig, success: (result) {
      if (result['errcode'] != 200) {
        tip.call(result['errmsg']);
      } else {
        delay();
      }
    }, failure: (error) {
      print('object_error=>$error');
    });
  }

  registerCode(Function tip) async {
    var phone = phoneMap['phoneNum'] ?? '';
    if (phone.isEmpty || !regPhone(phone)) {
      tip.call('请输入正确的手机号码');
      return;
    }
    var password = phoneMap['password'] ?? '';
    if (password.isEmpty) {
      tip.call('请输入密码');
      return;
    }

    var passwordConfig = phoneMap['passwordConfig'] ?? '';
    if (passwordConfig.isEmpty || passwordConfig != password) {
      tip.call('确认密码不能为空或者需密码不一致');
      return;
    }

    await registerApi.postRegister('${phoneMap['areaCode']}_$phone', '', password, passwordConfig, success: (result) {
      if (result['errcode'] != 200) {
        tip.call(result['errmsg']);
      } else {
        // userID
        delay();
      }
    }, failure: (error) {
      print('object_error=>$error');
    });
  }

  var codeStr = '获取验证码';
  Timer? timer;

  delay() {
    var time = 10;
    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      time--;
      if (time <= 0) {
        codeStr = '获取验证码';
        timer.cancel();
      } else {
        codeStr = '$time';
      }
      notifyListeners();
    });
  }

  @override
  void dispose() {
    if (timer != null) {
      timer?.cancel();
    }
    super.dispose();
  }

  var userID = '';

  conformVerifyCode(
    String userID,
    map,
    Function tip,
  ) {
    var verificationCode = map['verificationCode'] ?? '';
    var mapParam = {};
    mapParam['Code'] = map['verificationCode'];
    mapParam['Userid'] = map[''];
    mapParam['Mobile'] = map['phoneNum'];
    mapParam['Email'] = map[''];
    if (verificationCode.isNotEmpty) {
      registerApi.conformVerifyCode(mapParam, success: (result) {
        print('object=>$result');
      }, failure: (error) {
        print('object=>$error');
      });
    } else {
      tip.call('验证码不能为空');
    }
  }
}
